{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "osmnx version 0.15.1\n"
     ]
    }
   ],
   "source": [
    "import osmnx as ox\n",
    "import time\n",
    "from shapely.geometry import Polygon\n",
    "import os\n",
    "\n",
    "def save_graph_shapefile_directional(G, filepath=None, encoding=\"utf-8\"):\n",
    "    # default filepath if none was provided\n",
    "    if filepath is None:\n",
    "        filepath = os.path.join(ox.settings.data_folder, \"graph_shapefile\")\n",
    "\n",
    "    # if save folder does not already exist, create it (shapefiles\n",
    "    # get saved as set of files)\n",
    "    if not filepath == \"\" and not os.path.exists(filepath):\n",
    "        os.makedirs(filepath)\n",
    "    filepath_nodes = os.path.join(filepath, \"nodes.shp\")\n",
    "    filepath_edges = os.path.join(filepath, \"edges.shp\")\n",
    "\n",
    "    # convert undirected graph to gdfs and stringify non-numeric columns\n",
    "    gdf_nodes, gdf_edges = ox.utils_graph.graph_to_gdfs(G)\n",
    "    gdf_nodes = ox.io._stringify_nonnumeric_cols(gdf_nodes)\n",
    "    gdf_edges = ox.io._stringify_nonnumeric_cols(gdf_edges)\n",
    "    # We need an unique ID for each edge\n",
    "    gdf_edges[\"fid\"] = gdf_edges.index\n",
    "    # save the nodes and edges as separate ESRI shapefiles\n",
    "    gdf_nodes.to_file(filepath_nodes, encoding=encoding)\n",
    "    gdf_edges.to_file(filepath_edges, encoding=encoding)\n",
    "\n",
    "print(\"osmnx version\",ox.__version__)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Download network in a boundingbox"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--- 0.5374898910522461 seconds ---\n"
     ]
    }
   ],
   "source": [
    "bounds = (18.029122582902115, 18.070836297501724, 59.33476653724975, 59.352622230576124)\n",
    "x1,x2,y1,y2 = bounds\n",
    "boundary_polygon = Polygon([(x1,y1),(x2,y1),(x2,y2),(x1,y2)])\n",
    "G = ox.graph_from_polygon(boundary_polygon, network_type='drive')\n",
    "start_time = time.time()\n",
    "save_graph_shapefile_directional(G, filepath='./stockholm')\n",
    "print(\"--- %s seconds ---\" % (time.time() - start_time))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Download network in a city"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "place =\"Stockholm, Sweden\"\n",
    "G = ox.graph_from_place(place, network_type='drive', which_result=2)\n",
    "save_graph_shapefile_directional(G, filepath='stockholm')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Download network with a boundary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Download by a boundary polygon in geojson\n",
    "import osmnx as ox\n",
    "from shapely.geometry import shape\n",
    "json_file = open(\"stockholm_boundary.geojson\")\n",
    "import json\n",
    "data = json.load(json_file)\n",
    "boundary_polygon = shape(data[\"features\"][0]['geometry'])\n",
    "G = ox.graph_from_polygon(boundary_polygon, network_type='drive')\n",
    "save_graph_shapefile_directional(G, filepath='stockholm')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python (ox)",
   "language": "python",
   "name": "ox"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
